﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Application.Permisos.ServiceAgent;
using System.Windows.Forms;

namespace Cesave.Presentation.Permisos.WinForms.Client.ViewModels.Permisos
{
	public partial class ExpedicionViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<CultivosNumero> _cultivosNumeros;

		/// <summary>
		/// 
		/// </summary>
		private CultivosNumero _cultivosNumero;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<CultivosNumero> CultivosNumeros
		{
			get
			{
				return _cultivosNumeros;
			}
			private set
			{
				_cultivosNumeros = value;
				if (_cultivosNumeros != null)
					RaisePropertyChanged("CultivosNumeros");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public CultivosNumero CultivosNumero
		{
			get
			{
				return _cultivosNumero;
			}
			private set
			{
				_cultivosNumero = value;
				if (_cultivosNumero != null)
					RaisePropertyChanged("CultivosNumero");
			}
		}

		#endregion

		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		private void LoadCultivosNumeros()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					using (PermisosModuleServiceWrapper service = new PermisosModuleServiceWrapper())
					{
						e.Result = service.FindCultivosNumerosByStatus(true);
					}
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						CultivosNumeros = e.Result as List<CultivosNumero>;
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentCultivosNumero(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if ((bindingSource != null) && (bindingSource.Current != null))
				CultivosNumero = bindingSource.Current as CultivosNumero;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnCultivosNumerosPropertyChanged()
		{
			_expedicionView.cultivosNumerosBindingSource.DataSource = CultivosNumeros;
			_expedicionView.cultivosNumerosBindingSource.CurrentChanged += SetCurrentCultivosNumero;
			SetCurrentCultivosNumero(_expedicionView.cultivosNumerosBindingSource, new EventArgs());
		}

		private void OnCultivosNumeroPropertyChanged()
		{

		}

		#endregion
	}
}
